package com.test21.nowcoder;

public class Demo {
	public static void main(String[] args) {
		int[] arr = {4,4,10,21};
		System.out.println(new Demo().getPos(arr,4,4));
	}
    public int getPos(int[] A, int n, int val) {
    	return this.search(0, n-1, A, val);
    }
	public int search(int start,int end,int[] arr,int key){
		int center = (start+end)/2;
		if (end < start) {
			return -1;
		}

		int tmp = arr[center];
		
		if (tmp==key) {
			if (center-1>=0 && arr[center-1]==key) {
				end = center-1;
			}else{
				return center;
			}
		}else if(tmp>key){
			end = center-1;
		}else{
			start = center+1;
		}
		return this.search(start, end, arr, key);
	}
}
